#include "stdafx.h"
#include "TrainingCase.h"


CTrainingCase::CTrainingCase(void)
{
}


CTrainingCase::~CTrainingCase(void)
{
}

CTrainingCase::CTrainingCase( EOBJTYPE tEnvRoundMe[ ALL_DIRECTION_COUNT ] )
{
	memcpy( m_tEnvRoundMe, tEnvRoundMe, sizeof(EOBJTYPE)*ALL_DIRECTION_COUNT );
	memset( m_nInputSignalSequence, 0, sizeof(m_nInputSignalSequence) );
	memset( m_nCurrentSignal, 0, sizeof(m_nCurrentSignal) );
	Complex CONST_C0, CONST_C1;

	for( int j=0; j<ALL_DIRECTION_COUNT; j++ )
	{
		if( m_tEnvRoundMe[j] == EOBJTYPE_WATER )
		{
			m_nInputSignalSequence[0][j] = c1;
		}
		else if ( m_tEnvRoundMe[j] == EOBJTYPE_FOOD )
		{
			m_nInputSignalSequence[1][j] = c1;
		}
		else if ( m_tEnvRoundMe[j] == EOBJTYPE_CREATURE )
		{
			m_nInputSignalSequence[2][j] = c1;
		}
	}
}

Complex CTrainingCase::GetSignalForNervus( int nNervusIndex )
{
	Complex CONST_C0;
	if( m_nCurrentSignal[nNervusIndex] < ALL_DIRECTION_COUNT )
		return m_nInputSignalSequence[nNervusIndex][m_nCurrentSignal[nNervusIndex]++];
	else
		return c0;
}

RESULT_JUDGMENT CTrainingCase::CompareNervusOutputSignal( void )
{
	DIRECTION tDir=DIR_UP;

	int nLeftPulse = m_nOutputSignal[2], nRightPulse = m_nOutputSignal[3], nFrontPulse = m_nOutputSignal[0], nBackPulse = m_nOutputSignal[1];

	if( nFrontPulse == nBackPulse ) //not move
	{
		int i;
		for( i=0; i<ALL_DIRECTION_COUNT; i++ )
			if( m_tEnvRoundMe[i] == EOBJTYPE_FOOD )
				return RES_COMMON;
		for( i=0; i<ALL_DIRECTION_COUNT; i++ )
			if( m_tEnvRoundMe[i] != EOBJTYPE_CREATURE )
				break;

		if( i == ALL_DIRECTION_COUNT )
			return RES_GOOD;
		else
			return RES_COMMON;
	}

	if( nLeftPulse >= nRightPulse ) //turn left
	{
		switch( nLeftPulse - nRightPulse )
		{
		case 0: //UP
			tDir = DIR_UP;
			break;
		case 1: //LEFTUP
			tDir = DIR_LEFT_UP;
			break;
		case 2: //LEFT
			tDir = DIR_LEFT;
			break;
		case 3: //LEFTDOWN
			tDir = DIR_LEFT_DOWN;
			break;
		case 4: //DOWN
			tDir = DIR_DOWN;
			break;
		case 5: //RIGHTDOWN
			tDir = DIR_RIGHT_DOWN;
			break;
		case 6: //RIGHT
			tDir = DIR_RIGHT;
			break;
		case 7: //RIGHTUP
			tDir = DIR_RIGHT_UP;
			break;
		}
	}

	if( nLeftPulse < nRightPulse ) //turn right
	{
		switch( nRightPulse - nLeftPulse )
		{
		case 0: //UP
			tDir = DIR_UP;
			break;
		case 1: //RIGHTUP
			tDir = DIR_RIGHT_UP;
			break;
		case 2: //RIGHT
			tDir = DIR_RIGHT;
			break;
		case 3: //RIGHTDOWN
			tDir = DIR_RIGHT_DOWN;
			break;
		case 4: //DOWN
			tDir = DIR_DOWN;
			break;
		case 5: //LEFTDOWN
			tDir = DIR_LEFT_DOWN;
			break;
		case 6: //LEFT
			tDir = DIR_LEFT;
			break;
		case 7: //LEFTUP
			tDir = DIR_LEFT_UP;
			break;
		}
	}

	if( nFrontPulse < nBackPulse ) //go back
	{
		switch( tDir )
		{
		case DIR_DOWN:
			tDir = DIR_UP;
			break;
		case DIR_LEFT_DOWN:
			tDir = DIR_RIGHT_UP;
			break;
		case DIR_LEFT: 
			tDir = DIR_RIGHT;
			break;
		case DIR_LEFT_UP: 
			tDir = DIR_RIGHT_DOWN;
			break;
		case DIR_UP: 
			tDir = DIR_DOWN;
			break;
		case DIR_RIGHT_UP: 
			tDir = DIR_LEFT_DOWN;
			break;
		case DIR_RIGHT:
			tDir = DIR_LEFT;
			break;
		case DIR_RIGHT_DOWN: 
			tDir = DIR_LEFT_UP;
			break;
		}
	}

	//TRACE("env [%d %d %d %d %d %d %d %d], tDir %d, %d\n",m_tEnvRoundMe[0], m_tEnvRoundMe[1], m_tEnvRoundMe[2], m_tEnvRoundMe[3], m_tEnvRoundMe[4], m_tEnvRoundMe[5], m_tEnvRoundMe[6], m_tEnvRoundMe[7], tDir, m_tEnvRoundMe[tDir]);
	if( m_tEnvRoundMe[ tDir ] == EOBJTYPE_CREATURE )
		return RES_BAD;
	if( m_tEnvRoundMe[ tDir ] == EOBJTYPE_FOOD )
		return RES_GOOD;
	if( m_tEnvRoundMe[ tDir ] == EOBJTYPE_WATER )
	{
		int i;
		for( i=0; i<ALL_DIRECTION_COUNT; i++ )
			if( m_tEnvRoundMe[i] == EOBJTYPE_FOOD )
				break;
		if( i != ALL_DIRECTION_COUNT )
			return RES_COMMON;
		else
			return RES_GOOD;
	}
	return RES_BAD;
}

